// 平台部署
app.controller('platformCtrl', function ($scope, $http, $location, $timeout) {
  //页面通用初始化
  pageInit('platform');
  // 脱机
  $scope.isOffline = isOffline;
  //左侧导航用
  menuInit($scope, $http);
  $scope.menuIndex = 4.1;

  // 是否包含其他必须操作的按钮，依次为【多数据源数据比对】
  $scope.hasSpecialBtn = {
    "/deploy/db/multi": 0
  }
  // 判断是否存在需要执行的其他按钮
  $scope.checkSpecialBtn = function () {
    for (var key in $scope.hasSpecialBtn)
      if ($scope.hasSpecialBtn[key] == 1)
        return true;
    return false;
  }

  // 是否包含需要提示操作的按钮，依次为【导入内控报表】【导入全面风险报表】
  $scope.hasSpecialBtn2 = {
    "/deploy/report/management": 0,
    "/deploy/report/fsk": 0
  }
  // 判断是否存在需要提示操作的其他按钮
  $scope.checkSpecialBtn2 = function () {
    for (var key in $scope.hasSpecialBtn2)
      if ($scope.hasSpecialBtn[key] == 1)
        return true;
    return false;
  }


  // 阻止刷新
  // $(window).bind('beforeunload', function () {
  //   if ($scope.hasChange)
  //     return '离开该页面，手动更改的配置都将还原，确认离开？';
  // });
  init();

  //按照应用分类
  function handleRowData(rowData) {
    var data = {};
    for (var key in rowData) {
      var type = key.split("/")[0];
      if (!data[type]) {
        data[type] = {};
      }
      data[type][key] = rowData[key];
    }
    return data;
  }

  function init() {
    $scope.hasChange = true;
    //初始化预览
    $http.get('/deploy/init').then(function (response) {
      if (response.status == 200) {
        var message = response.data;
        if (message.code == 0) {
          $scope.types = handleRowData(message.data);
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
    //初始化按钮
    $http.get('/deploy/button').then(function (response) {
      if (response.status == 200) {
        var message = response.data;
        if (message.code == 0) {
          $scope.btns = message.data;
          for (var i = 0; i < $scope.btns.length; i++) {
            // 将必要执行按钮标记为未执行
            for (var key in $scope.hasSpecialBtn) {
              if ($scope.btns[i].url == key)
                $scope.hasSpecialBtn[key] = 1;
            }
            // 将提示操作按钮标记为未执行
            for (var key in $scope.hasSpecialBtn2) {
              if ($scope.btns[i].url == key)
                $scope.hasSpecialBtn2[key] = 1;
            }
          }
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  }

  // 平台部署
  $scope.deploy = function () {
    $scope.isDeploy = true;
    $http.get('/deploy/start').then(function (response) {
      $scope.isDeploy = false;
      if (response.status == 200) {
        var message = response.data;
        $scope.hasChange = false;
        if (message.code == 0) {
          // $location.path('done');
          $scope.hasChange = false;
          $.showNotice(message.msg);
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $scope.isDeploy = false;
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  };

  // 数据源替换
  $scope.replace = function () {
    $scope.isReplace = true;
    $http.get('/deploy/replace').then(function (response) {
      $scope.isReplace = false;
      if (response.status == 200) {
        var message = response.data;
        if (message.code == 0) {
          $('#platform-console').modal('show');
          $scope.logs = message.data;
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $scope.isReplace = false;
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  };

  // 手动修改
  $scope.editConfig = function (type, fileName, key, value) {
    $('#platform-edit').modal('show');
    $scope.nowType = type;
    $scope.nowFileName = fileName;
    $scope.nowKey = key;
    $scope.nowValue = value;
  }

  // 执行修改
  $scope.doChange = function () {
    $http.post('/deploy/modify', {
      path: $scope.nowFileName,
      key: $scope.nowKey,
      value: $scope.nowValue
    }).then(function (response) {
      if (response.status == 200) {
        var message = response.data;
        if (message.code == 0) {
          $scope.types[$scope.nowType][$scope.nowFileName][$scope.nowKey] = $scope.nowValue;
          $scope.hasChange = true;
          $('#platform-edit').modal('hide');
          $.showNotice(message.msg);
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  }

  // 其他操作
  $scope.doSomething = function (url) {
    $http.get(url).then(function (response) {
      if (response.status == 200) {
        var message = response.data;
        if (message.code == 0) {
          $.showNotice(message.msg);
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
    // 将必要执行按钮标记为已执行
    for (var key in $scope.hasSpecialBtn) {
      if (url == key)
        $scope.hasSpecialBtn[key] = 2;
    }
    // 将提示操作按钮标记为已执行
    for (var key in $scope.hasSpecialBtn2) {
      if (url == key)
        $scope.hasSpecialBtn2[key] = 2;
    }
  };


  // 控制跳转
  $scope.confirmGo = function (path) {
    // 忽略修改
    $scope.justGo = function () {
      $('#platform-confirm').modal('hide');
      $scope.hasChange = false;
      // alert(path);
      $scope.confirmGo(path);
    }
    // 忽略提示按钮的操作
    $scope.justGo2 = function (id) {
      var key = '/deploy/report/' + id;
      $('#platform-' + id).modal('hide');
      $scope.hasSpecialBtn2[key] = 2;
      $scope.confirmGo(path);
    }

    // 平台部署
    if ($scope.hasChange) {
      $('#platform-confirm').modal('show');
    }
    else {
      // 提示操作按钮
      for (var key in $scope.hasSpecialBtn2) {
        if ($scope.hasSpecialBtn2[key] == 1) {
          var id = key.substr(key.lastIndexOf('/') + 1);
          $('#platform-' + id).modal('show');
          return;
        }
      }
      // 跳转
      $location.path(path);
      return;
    }
  }

});